﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DLL;
using PagedList;

namespace QuanLyBanLe
{
    public partial class FormProductSalePriceNotGood : Form
    {
        private ProductService m_ProductService;
        private List<pGetProductHasPriceIsNotGood_Result> m_Products;
        private int m_PageNumber;
        private int m_PageSize;
        private int m_MaxPage;
        private int m_TotalRecord;
        private bool m_EnabledEvent;

        public FormProductSalePriceNotGood()
        {
            InitializeComponent();
            m_PageSize = 20;
            m_PageNumber = 1;
            m_EnabledEvent = false;
            m_ProductService = new ProductService();
            m_Products = new List<pGetProductHasPriceIsNotGood_Result>();
        }

        private void FormProductSalePriceNotGood_Load(object sender, EventArgs e)
        {
            dtgProducts.AutoGenerateColumns = false;
            BindDataSource();
            this.Text += FormMain.GetShopInfo();
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            m_PageNumber = 1;
            ChangePage();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_PageNumber > 1 ? m_PageNumber - 1 : m_PageNumber;
            ChangePage();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_PageNumber < m_MaxPage ? m_PageNumber + 1 : m_PageNumber;
            ChangePage();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_MaxPage;
            ChangePage();
        }

        private void mnrPageIndex_ValueChanged(object sender, EventArgs e)
        {
            if (m_EnabledEvent)
            {
                m_PageNumber = (int)mnrPageIndex.Value;
                ChangePage();
            }
        }

        private void GetData()
        {
            m_Products = m_ProductService.GetProductHasPriceNotGood();
        }

        private void BindDataSource()
        {
            m_EnabledEvent = false;
            GetData();
            IPagedList<pGetProductHasPriceIsNotGood_Result> pagedList = m_Products.ToPagedList(m_PageNumber, m_PageSize);
            dtgProducts.DataSource = pagedList.ToList();
            m_TotalRecord = pagedList.TotalItemCount;
            m_MaxPage = pagedList.PageCount;
            if (m_TotalRecord > 0)
            {
                lblTotalPage.Text = "/" + m_MaxPage.ToString();
                m_EnabledEvent = false;
                mnrPageIndex.Minimum = 1;
                mnrPageIndex.Enabled = true;
                mnrPageIndex.Maximum = m_MaxPage;
                mnrPageIndex.Value = m_PageNumber;
                SetEnabledForPagingNavigator();
            }
            else
            {
                m_PageNumber = 0;
                mnrPageIndex.Minimum = 0;
                mnrPageIndex.Value = 0;
                mnrPageIndex.Enabled = false;
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = false;
                btnNext.Enabled = false;
            }
            m_EnabledEvent = true;
        }

        private void SetEnabledForPagingNavigator()
        {
            if (m_PageNumber == m_MaxPage)
            {
                if (m_PageNumber == 1)
                {
                    btnFirst.Enabled = false;
                    btnPrev.Enabled = false;
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
                else
                {
                    btnFirst.Enabled = true;
                    btnPrev.Enabled = true;
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else if (m_PageNumber == 1)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
            }
            else
            {
                btnFirst.Enabled = true;
                btnPrev.Enabled = true;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
            }
        }

        private void ChangePage()
        {
            IPagedList<pGetProductHasPriceIsNotGood_Result> pagedList = m_Products.ToPagedList(m_PageNumber, m_PageSize);
            dtgProducts.DataSource = pagedList.ToList();

            // Prevent changePage call second time from mnrPageIndex_ValueChanged
            m_EnabledEvent = false;
            mnrPageIndex.Value = m_PageNumber;
            m_EnabledEvent = true;

            m_TotalRecord = pagedList.TotalItemCount;
            m_MaxPage = pagedList.PageCount;
            lblTotalPage.Text = "/" + m_MaxPage.ToString();

            SetEnabledForPagingNavigator();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
